{extend name="public:head"}
{block name="main"}

<form class="layui-form" action="" style="margin-top: 50px" lay-filter="formAll">

    <div class="layui-form-item">
        <label class="layui-form-label">推广域名</label>
        <div class="layui-input-block">
            <input type="text" name="host" required placeholder="请输入域名" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text" id="signs">
        <label class="layui-form-label">限定标识码</label>
        <div class="layui-input-block">
            <textarea name="desc" placeholder="用%隔开每一个标识码" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <!--<a href="/login/Preview/index" class="layui-btn layui-btn-warm" lay-submit lay-filter="yulan">预览</a>-->
        </div>
    </div>

    <table id="url" lay-filter="url"></table>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
</form>

<script>
    //Demo
    layui.use(['form','table'], function(){
        var form = layui.form;
        var table = layui.table;
        var token = layui.data('cache');

        //  初始化值
        $.post({
            url:"/login/Identification/Initialization?type=sign",
            type:"get",
            headers:{"token":token.token},
            dataType:"json",
            success:function (res) {
                form.val("formAll",{
                    "desc":res.data.sign,
                    "host":res.data.host,
                })
                if(res.code == 400){
                    layer.msg("初始失败，请刷新")
                }
            },
            error:function (error) {
                if(error.code == 500){
                    layer.msg('权限不够');
                }
            }
        })
        //监听提交
        form.on('submit(formDemo)', function(data){
            var textData = JSON.stringify(data.field.desc);
            var hostData = JSON.stringify(data.field.host);
            var reg=/"/g;
            var desc=textData.replace(reg,'');
            var host=hostData.replace(reg,'');
            $.post({
                url:"/login/Identification/identificationAndIpApi",
                type:"post",
                headers:{"token":token.token},
                dataType:"json",
                data:{"data":desc,"type":"sign", 'host': host},
                success:function (res) {
                    console.log(res.code)
                    if(res.code == 200){
                        layer.msg("更新成功")
                        table.reload('url', {
                            url: '/login/Identification/allUrl'
                            ,headers: {"token":token.token}
                        });
                    }
                    if(res.code == 400){
                        layer.msg("更新失败")
                    }
                },
                error:function (error) {
                    if(error.responseJSON.code == 500){
                        layer.msg('更新失败！！！权限不够');
                    }
                    if(error.responseJSON.code == 402){
                        layer.msg('更新失败！！！最多有30个标识码');

                    }
                    if(error.responseJSON.code == 409){
                        layer.msg('更新失败！！！参数错误，链接必须加上http或者https');

                    }
                }
            })
            return false;
        });
        // 把所有推广链接都显示出来
        table.render({
            elem: '#url'
            ,height: 312
            ,toolbar: true
            ,url: '/login/Identification/allUrl' //数据接口
            ,headers: {"token":token.token}
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: '编号',sort: true, width:250}
                ,{field: 'sign', title: '标识码', width:250}
                ,{field: 'url', title: '推广链接', width:500}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150 , align: 'center'}
            ]]
        });

        // 鼠标移动提示框
        var tip_index = 0;
        $(document).on('mouseenter', '#signs', function(){
            tip_index = layer.tips('添加允许推广的表示，用%号隔开，例如：biaoshi001%biaoshi001', '#signs', {time: 0});
        }).on('mouseleave', '#signs', function(){
            layer.close(tip_index);
        });

        table.on('tool(url)', function(obj){
            var token = layui.data('cache') // 获取缓存中的token
            if(obj.event == 'del'){
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    var arr = Array()
                    arr[0] = obj.data
                    var signData = arr[0].sign
                    // 向服务端发送删除指令
                    $.ajax({
                        url:"/login/Identification/delSignOrIp",
                        type:"get",
                        headers:{"token":token.token},
                        dataType:"json",
                        data:{
                            'data':signData,
                            'type':'sign'
                        },
                        success:function (res) {
                            if(res.code == 200){
                                layer.msg('删除成功！！！');
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                        },
                        error:function (error) {
                            if(error.responseJSON.code == 500){
                                layer.msg('删除失败！！！权限不够');
                            }
                        }
                    })
                });
            }
        });
    });
</script>

{/block}